// pages/class/attendance/index.js
const $class = require("../../../api/class").API
const $api = require("../../../api/mine").API

Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[],
    sign_year:new Date().getFullYear(),
    sign_month:new Date().getMonth()+1,
    sign_date:new Date().getDate(),
    currentDate: new Date().getTime(),
    minDate: new Date().getTime(),
    showchoosetime:false,
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      } else if (type === 'month') {
        return `${value}月`;
      }
      return value;
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var t = this
    if(options.type == 1 ){
      t.setData({
        type:options.type
      })
    }else if(options.type == 2 ){
      t.setData({
        type:options.type,
        class_id:options.class_id,
        currentDate:parseInt( options.currentDate),
        sign_year:new Date(parseInt( options.currentDate)*1).getFullYear(),
        sign_month:new Date(parseInt( options.currentDate)*1).getMonth()+1,
        sign_date:new Date(parseInt( options.currentDate)*1).getDate(),
      })
    }
   
  },
  onReady(){
    var t = this
    if(t.data.type==1){
      t.getPrincipalAttendanceList()
    }
    if(t.data.type==2){
      t.getClassAttendance()

    }
  },
  getClassAttendance(){
    var t = this,
    data = {
      class_id:t.data.class_id,
      sign_year:t.data.sign_year,
      sign_month:t.data.sign_month,
      sign_date:t.data.sign_date
    }
    $class.getClassAttendance(data).then(res=>{
      console.log(res)
      t.setData({
        list:res.data.absence
      })
    })
  },
  onClose(){
    var t= this
    t.setData({
      showchoosetime:false
    })
  },
  choosetime(){
    var t= this
    t.setData({
      showchoosetime:true
    })
  },
  choosed(e){
    var t = this
    t.setData({
      showchoosetime:false
    })
    var time = e.detail
    if (time) {
      var oDate = new Date(time * 1),
        oYear = oDate.getFullYear(),
        oMonth = oDate.getMonth() + 1,
        oDay = oDate.getDate()
        t.setData({
          sign_year:oYear,
          sign_month:oMonth,
          sign_day : oDay,
          currentDate:time,
        })
    }
    t.onReady()

  },
  timechange(e){
    var t= this
    console.log(e)
  },
  // 园长功能 - 考勤管理 -考勤记录列表
  getPrincipalAttendanceList() {
    var t = this,
    data = {
      item_id:wx.getStorageSync('memberinfo').member_id,
      sign_year:t.data.sign_year,
      sign_month:t.data.sign_month,
      sign_date:t.data.sign_date,
      institution_id:wx.getStorageSync('TeacherClassinfo').institution_id
    }
    $api.getPrincipalAttendanceList(data).then(res=>{
      console.log(res)
      t.setData({
        list:res.data.list
      })
    })
  }
})